package com.ittao.taoaicodemother.config;

import cn.hutool.core.util.StrUtil;
import dev.langchain4j.community.store.memory.chat.redis.RedisChatMemoryStore;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Redis 的持久化对话记忆
 */
@Configuration
@ConfigurationProperties(prefix = "spring.data.redis")
@Data
public class RedisChatMemoryStoreConfig {

    /**
     * Redis服务器主机名
     */
    private String host;

    /**
     * Redis服务器端口号
     */
    private int port;

    /**
     * Redis服务器密码
     */
    private String password;

    /**
     * Redis 过期时间，单位秒
     */
    private long ttl;

    /**
     * Redis数据库索引
     */
    private int database;

    /**
     * 创建 RedisChatMemoryStore Bean
     *
     * @return RedisChatMemoryStore 实例
     */
    @Bean
    public RedisChatMemoryStore redisChatMemoryStore() {
        RedisChatMemoryStore.Builder builder = RedisChatMemoryStore.builder()
                .host(host)
                .port(port)
                .password(password)
                .ttl(ttl);
        if (StrUtil.isNotBlank(password)) {
            builder.user("default");
        }
        return builder.build();
    }
}
